package ocbn.remotelab.util;

import java.util.Vector;

/**
 *
 * @author Ke
 */
public class Enum {
        /**
         * A String containing the name of the state
         */
        private final String name;
        /**
         * A Vector object that stores all the instances of this class
         */
        private static Vector instances;

        /**
         * Constructs an instance of State from the name, the description and
         * the importance.
         *
         * @param name          A String containing the name of the State object
         * @param description   A String containing the description of the State
         * @param isImportant   A boolean indicating whether events in this
         *                      State is considered important
         */
        protected Enum(String name) {
            this.name = name;
            addInstance(this);
        }

        /**
         * Adds an instance of this class to the instance Vector.
         *
         * @param instance  A State object that is a new instance of this class
         */
        private static void addInstance(Enum instance) {
            if (instances == null) {
                instances = new Vector();
            }
            instances.addElement(instance);
        }

        /**
         * Retrieves the name of the State.
         *
         * @return  A String containing the name of the State object
         */
        public String name() {
            return name;
        }

        /**
         * Converts the State to a String.
         *
         * @return  A String representation of this State
         */
        public String toString() {
            return name();
        }

        /**
         * Retrieves a State instance by its name.
         *
         * @param name  The name of the desired State instance
         * @return      The State instance if it is found, otherwise null
         */
        public static Enum valueOf(String name) {
            for (int i = 0; i < instances.size(); i++) {
                if (((Enum) (instances.elementAt(i))).name().equals(name)) {
                    return (Enum) (instances.elementAt(i));
                }
            }
            return null;        // Not found
        }
}
